<template>
    <div class="add">
    <el-row class="head">
        <el-col >——新增维修人员/维修人员</el-col>
    </el-row>

        <el-row class="title">
            <el-col>新增维修人员</el-col>
        </el-row>
        <el-form  :model="admin" label-width="100px" class="formrule" :rules="rules" ref="ruleform">

            <el-form-item label="维修分类" prop="typeId">
                <el-select v-model="admin.typeId" placeholder="请选择"  >
                    <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="账号" prop="username">
                <el-input v-model="admin.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="admin.password"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
                <el-input v-model="admin.name"></el-input>
            </el-form-item>

            <el-form-item label="手机" prop="mobile">
                <el-input v-model="admin.mobile"></el-input>
            </el-form-item>

            <el-form-item label="部门" prop="company">
                <el-input v-model="admin.company"></el-input>
            </el-form-item>

            <el-form-item label="设为管理员">
                <el-switch v-model="admin.delivery"></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="onSubmit(admin)">新增</el-button>
                <el-button @click="resetForm()">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "add",
        data(){
            return{

                admin:{
                    role:'1',
                    typeId:'',
                    username:'',
                    password:'',
                    name:'',
                    mobile:'',
                    company:'',
                    delivery:'false',
                },

                options:[],
                rules: {
                    typeId: [
                        {required: true, message: '请选择维修分类', trigger: 'change'}
                    ],
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                        {min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur'}
                    ],
                    mobile: [
                        {required: true, message: '请输入你的手机号码', trigger: 'blur'},
                        {min: 11, max: 11, message: '长度为11位的数字', trigger: 'blur'}
                    ],
                    company: [
                        {required: true, message: '请输入公司', trigger: 'blur'},
                        {min: 2, max: 18, message: '长度在 2 到 18 个字符', trigger: 'blur'}
                    ],
                }
            }
        },
        created()
        {
            this.$axios.post("/api/v1/user/seltypeall").then(result => {

                return result.data;
            })
                .then(data1 => {
                    this.options=data1;
                });
        },
        methods:{
            onSubmit(admin){

                this.$refs.ruleform.validate((valid) => {
                    if (valid) {
                        if (admin.delivery===true)
                            admin.role=5;
                        else
                            admin.role=1;
                        this.$axios.post("/api/v1/user/super/create",admin).then(result => {

                            return result.data;
                        })
                            .then(data => {
                                if (data.data===0)
                                    this.$message({
                                        type:'success',
                                        message:data.msg,
                                    });
                                else
                                    this.$message({
                                        type:'error',
                                        message:data.msg,
                                    });
                                this.resetForm();
                            });
                    } else {
                        return false;
                    }
                });

            },
            resetForm(){
                   this.admin.typeId='';
                   this.admin.delivery=false;
                   this.admin.mobile='';
                   this.admin.username='';
                   this.admin.password='';
                   this.admin.name='';
                   this.admin.role=1;
                   this.admin.company='';
            }
        }
    }
</script>

<style scoped>
    .el-input{
        width: 600px;
    }
</style>